package test.format_test;

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class ForNet {
    public static void main(String[] args) throws UnknownHostException {
        InetAddress host1 = InetAddress.getLocalHost();
        System.out.println(host1);
    }

}


class SocketTCPServer{
    public static void main(String[] args) throws IOException {
        //在9999端口监听
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务端在9999端口监听，等待连接");
        Socket socket = serverSocket.accept();
        System.out.println("服务器端socket =:"+socket.getClass());
        InputStream inputStream = socket.getInputStream();


        FileOutputStream fileOutputStream = new FileOutputStream("./generate/generateCar.jpg");
        byte[] buf2 = new byte[8];
        int readLen = 0;
        while((readLen = inputStream.read(buf2))!=-1){
            fileOutputStream.write(buf2,0,readLen);
        }
        fileOutputStream.close();
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("已收到图片".getBytes());
        socket.shutdownOutput();
        outputStream.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
    }
}

class SocketTCPClient{
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println("客户端 socket =:"+socket.getClass());
        OutputStream outputStream = socket.getOutputStream();


        File pic = new File("./resource/car.jpg");
        FileInputStream inpic = new FileInputStream(pic);
        byte[] buf2 = new byte[8];
        int readLen = 0;
        while((readLen=inpic.read(buf2))!=-1){
            outputStream.write(buf2,0,readLen);
        }
        socket.shutdownOutput();
        inpic.close();
        InputStream inputStream = socket.getInputStream();
        byte[] buf = new byte[1024];
        readLen =0;
        while ((readLen=inputStream.read(buf))!=-1){
            System.out.println(new String(buf,0,readLen));
        }
        inputStream.close();
        outputStream.close();
        socket.close();
        System.out.println("客户端退出");
    }
}


class UDPRecive{
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket(9998);
        byte[] buf = new byte[64*1024];
        DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
        datagramSocket.receive(datagramPacket);
        int length = datagramPacket.getLength();
        byte[] data = datagramPacket.getData();
        String s = new String(data,0,length);
        System.out.println(s);
        byte[] sendData =  "hello UDPSend".getBytes();
        datagramSocket.send(new DatagramPacket(sendData,sendData.length,InetAddress.getLocalHost(),9997));
        datagramSocket.close();
    }
}

class UDPSend{
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket(9997);
        byte[] data = "Hello UDPRecive".getBytes();
        DatagramPacket datagramPacket = new DatagramPacket(data, data.length, InetAddress.getLocalHost(), 9998);
        datagramSocket.send(datagramPacket);
        byte[] reciveData = new byte[64*1024];
        DatagramPacket packet = new DatagramPacket(reciveData, reciveData.length);
        datagramSocket.receive(packet);
        System.out.println(new String(packet.getData(),0,packet.getLength()));
        datagramSocket.close();
    }
}

class SocketTCPServerChat{
    public static void main(String[] args) throws IOException {
        //在9999端口监听
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务端在9999端口监听，等待连接");
        while(true) {
            Socket socket = serverSocket.accept();
            //Socket socket2 = serverSocket.accept();
            System.out.println("已连接");
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            System.out.println("获取输入流");
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
            Scanner scanner = new Scanner(System.in);
            String str = scanner.nextLine();
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(str.getBytes());
            socket.shutdownOutput();
            outputStream.close();
            br.close();
            //socket.close();
            if(str.equals("exit"))
                break;
        }
        serverSocket.close();
    }
}

class SocketTCPClientChat{
    public static void main(String[] args) throws IOException {
        while (true) {
            Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
            Scanner scanner = new Scanner(System.in);
            String str = scanner.nextLine();
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(str.getBytes());
            socket.shutdownOutput();
            InputStream inputStream = socket.getInputStream();
            byte[] buf = new byte[1024];
            int readLen = 0;
            while ((readLen = inputStream.read(buf)) != -1) {
                System.out.println(new String(buf, 0, readLen));
            }
            inputStream.close();
            outputStream.close();
            socket.close();
            if(str.equals("exit"))
                break;
        }
    }
}


class SocketTCPClientChat2{
    public static void main(String[] args) throws IOException {
        while (true) {
            Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
            Scanner scanner = new Scanner(System.in);
            String str = scanner.nextLine();
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write(str.getBytes());
            socket.shutdownOutput();
            InputStream inputStream = socket.getInputStream();
            byte[] buf = new byte[1024];
            int readLen = 0;
            while ((readLen = inputStream.read(buf)) != -1) {
                System.out.println(new String(buf, 0, readLen));
            }
            inputStream.close();
            outputStream.close();
            socket.close();
            if(str.equals("exit"))
                break;
        }
    }
}